
#ifndef _CORE_H_
#define _CORE_H_

#include "mcu.h"
#include "core/Const.h"
#include "core/CoreTimer.h"
#include "core/CoreHeap.h"
         
#include "ModulesInterFace.h"

extern const UInt8 null[];
#ifdef NULL
#undef NULL
#endif 

#define NULL    ((void *)null)

#define ENABLE_POWERDOWNCHECK    (gCoreFlag.PowerdownCheckEnable = TRUE ) 
#define DISABLE_POWERDOWNCHECK   (gCoreFlag.PowerdownCheckEnable = FALSE) 

#define UNLOCK_POWERDOWNCHECK    \
		gCoreFlag.PowerdownCheckEnable = PowerdownCheckFlg ; \
    }

#define LOCK_POWERDOWNCHECK   \
	{\
		unsigned char PowerdownCheckFlg = gCoreFlag.PowerdownCheckEnable;\
		gCoreFlag.PowerdownCheckEnable = FALSE

typedef struct
{
    BitField RunState:2;               
    BitField FirstPowerOn:1;           
    BitField PowerdownDetected:1;      
    BitField PowerdownConfirmed:1;     
    BitField EepromWriting:1;          

    BitField PowerdownCheckEnable:1;   
    BitField PowerdownSaveEnable:1;    

    BitField PowerdownLoadError:1;
    BitField EepromError:1;
}stCoreFlag;


typedef struct 
{
    UInt16 RandomSeed;
    UInt16 CounterMs;
    UInt8  CounterFor100Ms;
    UInt8  CounterFor500Ms;
    UInt8  CounterForSec;

    BitField EventPriovity:3;
    BitField EventReschedule:1;
    BitField RTEventReschedule:1;
    BitField RTEventRunning:1;

    stCoreTimer Timer;
}stCoreData;

#define EVENT_CORE_INIT       0x00
#define EVENT_CORE_IDLE       0x01
#define EVENT_CORE_10MS       0x02
#define EVENT_CORE_100MS      0x03  
#define EVENT_CORE_500MS      0x04  
#define EVENT_CORE_SEC        0x05

#define RUN_LOWPOW  0
#define RUN_INIT    1
#define RUN_NOCHECK 2
#define RUN_NORMAL  3

#define PRI_LEVELS 	8

#define PRI_REALTIME    0
#define PRI_HIGHEST     1
#define PRI_HIGH        2
#define PRI_ABOVENORMAL 3
#define PRI_NORMAL      4
#define PRI_UNDERNORMAL 5
#define PRI_LOW         6
#define PRI_LOWEST      7

typedef struct CoreEvent
{
    UInt8 AppIndex;
    UInt16 Code;
    TfpCoreEvent fpOnEventDone;
    stParam Param;
    struct CoreEvent * Next;
}stEvent;

void Core_TickService(void *pTimer);
void Core_Run(void);
UInt8 Core_EventRaise(UInt8 Priority,UInt16 EventCode,stParam *pEventParam,TfpCoreEvent fpOnEventDone);
UInt16 Core_GetRandom(UInt16 n);

extern stCoreFlag gCoreFlag;
extern stCoreData gCoreData;

#endif /* _CORE_H_ */
